home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Magazine / Warp3D / Warp3DExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-21  |  8.1 KB  |  373 lines

  1. #include <proto/exec.h>
  2. #include <proto/graphics.h>
  3. #include <proto/intuition.h>
  4. #include <clib/cybergraphics_protos.h>
  5. #include <proto/Warp3D.h>
  6.  
  7. #include <Warp3D/Warp3D.h>
  8. #include <cybergraphics/cybergraphics.h>
  9. #include <libraries/asl.h>
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <strings.h>
  15.  
  16. struct ScreenBuffer *buf1=0;
  17. struct ScreenBuffer *buf2=0;
  18. struct ScreenBuffer *buf3=0;
  19.  
  20. struct Window *mywindow=0;
  21. struct Screen *myscreen=0;
  22.  
  23. extern struct IntuitionBase *IntuitionBase;
  24. extern struct GfxBase *GfxBase;
  25. struct Library *CyberGfxBase=0;
  26.  
  27. #ifdef __PPC__
  28. struct Library *Warp3DPPCBase=0;
  29. #else
  30. struct Library *Warp3DBase=0;
  31. #endif
  32.  
  33. struct BitMap *bm=0;
  34.  
  35. W3D_Context *mycontext=0;
  36.  
  37. int mywidth,myheight;
  38.  
  39. int bufnum=0;
  40.  
  41. void ClearWindow(void)
  42. {
  43.   // Note: For fast 3D, it is prepared if the
  44.   // Screen is always completely filled by the 3D Engine,
  45.   // then no clearing is required... but in our case
  46.   // the clear is needed...
  47.  
  48.   ULONG video;
  49.   ULONG bpr;
  50.   ULONG handle;
  51.  
  52.   handle = LockBitMapTags(bm,
  53.          LBMI_BASEADDRESS, &video,
  54.          LBMI_BYTESPERROW, &bpr,
  55.      TAG_DONE);
  56.  
  57.   memset(video,0,bpr*480);
  58.  
  59.   UnLockBitMap(handle);
  60.  
  61. // Well, we should really go without this, in the final
  62. // program, filling the whole screen with 3D or such...
  63. // Clearing every frame is slow... and most 3D Engines
  64. // fill the whole screen with data... but this is just
  65. // a simple example !!!
  66. }
  67.  
  68. void SwitchBuffer(void)
  69. {
  70.  W3D_Scissor s = {0, 0, 0, 0};
  71.  
  72.  s.width=mywidth;
  73.  s.height=myheight;
  74.  
  75.  if (bufnum == 0)
  76.  {
  77.   bm = buf2->sb_BitMap;
  78.   mywindow->RPort->BitMap=bm;
  79.   ClearWindow();
  80.   W3D_SetDrawRegion(mycontext, bm, 0, &s);
  81.   buf2->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort = NULL;
  82.   while (!ChangeScreenBuffer(myscreen, buf2));
  83.   bufnum = 1;
  84.  }
  85.  else if (bufnum==1)
  86.  {
  87.   bm = buf3->sb_BitMap;
  88.   mywindow->RPort->BitMap=bm;
  89.   ClearWindow();
  90.   W3D_SetDrawRegion(mycontext, bm, 0, &s);
  91.   buf3->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort = NULL;
  92.   while (!ChangeScreenBuffer(myscreen, buf3));
  93.   bufnum=2;
  94.  }
  95.  else
  96.  {
  97.   bm = buf1->sb_BitMap;
  98.   mywindow->RPort->BitMap=bm;
  99.   ClearWindow();
  100.   W3D_SetDrawRegion(mycontext, bm, 0, &s);
  101.   buf1->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort = NULL;
  102.   while (!ChangeScreenBuffer(myscreen, buf1));
  103.   bufnum=0;
  104.  }
  105. }
  106.  
  107. void closeit(char *ch)
  108. {
  109.       if (buf1) FreeScreenBuffer(myscreen,buf1);
  110.       buf1=0;
  111.       if (buf2) FreeScreenBuffer(myscreen,buf2);
  112.       buf2=0;
  113.       if (buf3) FreeScreenBuffer(myscreen,buf3);
  114.       buf3=0;
  115.       if (mywindow) CloseWindow(mywindow);
  116.       mywindow=0;
  117.       if (myscreen) CloseScreen(myscreen);
  118.       myscreen=0;
  119.       if (IntuitionBase) CloseLibrary(IntuitionBase);
  120.       IntuitionBase=0;
  121.       if (GfxBase) CloseLibrary(GfxBase);
  122.       GfxBase=0;
  123.       if (CyberGfxBase) CloseLibrary(CyberGfxBase);
  124.             CyberGfxBase=0;
  125. #ifdef __PPC__
  126.             if (Warp3DPPCBase) CloseLibrary(Warp3DPPCBase);
  127.             Warp3DPPCBase=0;
  128. #else
  129.             if (Warp3DBase) CloseLibrary(Warp3DBase);
  130.             Warp3DBase=0;
  131. #endif
  132.             printf(ch);
  133.       exit(0);
  134. }
  135.  
  136. void init(int width, int height, int format)
  137. {
  138.     int flags,ModeID;
  139.     ULONG CError;
  140.     
  141.     mywidth=width;
  142.     myheight=height;
  143.  
  144.       IntuitionBase=OpenLibrary("intuition.library",0);
  145.     if (!IntuitionBase)
  146.         {
  147.           closeit("intuition.library could not be opened!\n");
  148.     }
  149.     GfxBase=OpenLibrary("graphics.library",0);
  150.     if (!GfxBase)
  151.     {
  152.       closeit("graphics.library could not be opened!\n");
  153.     }
  154.     CyberGfxBase=OpenLibrary("cybergraphics.library",0);
  155.     if (!CyberGfxBase)
  156.     {
  157.       closeit("cybergraphics.library could not be opened!\n");
  158.       exit(0);
  159.     }
  160. #ifdef __PPC__
  161.         Warp3DPPCBase=OpenLibrary("Warp3DPPC.library",4);
  162.     if (!Warp3DPPCBase)
  163. #else
  164.         Warp3DBase=OpenLibrary("Warp3D.library",0);
  165.     if (!Warp3DBase)
  166. #endif
  167.     {
  168.           closeit("Warp3D could not be opened!\n");
  169.     }
  170.         flags = W3D_CheckDriver();
  171.     if (flags & W3D_DRIVER_UNAVAILABLE)
  172.     {
  173.             closeit("No Warp3D Driver found!\n");
  174.     }
  175.  
  176.     ModeID = W3D_RequestModeTags(
  177.                    W3D_SMR_TYPE, W3D_DRIVER_3DHW,
  178.              W3D_SMR_SIZEFILTER,TRUE,
  179.              W3D_SMR_DESTFMT,format,
  180.              ASLSM_MinWidth,width,
  181.              ASLSM_MaxWidth,width+1,
  182.              ASLSM_MinHeight,height,
  183.              ASLSM_MaxHeight,height+1,
  184.              TAG_DONE);
  185.  
  186.    if (!ModeID)
  187.    {
  188.             closeit("No Screenmode requested!\n");
  189.    }
  190.  
  191.    myscreen = OpenScreenTags(NULL,
  192.        SA_Height, height,
  193.     SA_DisplayID, ModeID,
  194.     SA_Depth, 8,
  195.     SA_ShowTitle, FALSE,
  196.     SA_Draggable, FALSE,
  197.     TAG_DONE);
  198.  
  199.    if (!myscreen)
  200.    {
  201.             closeit("Screen could not get opened!\n");
  202.      }
  203.  
  204.    mywindow = OpenWindowTags(NULL,
  205.    WA_CustomScreen,myscreen,
  206.    WA_Activate,TRUE,
  207.    WA_Width,myscreen->Width,
  208.    WA_Height,myscreen->Height,
  209.    WA_Left,0,
  210.    WA_Top,0,
  211.    WA_Title,0,
  212.    WA_CloseGadget,FALSE,
  213.    WA_Backdrop,TRUE,
  214.    WA_Borderless,TRUE,
  215.      WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE|IDCMP_DELTAMOVE,
  216.    WA_Flags,WFLG_REPORTMOUSE|WFLG_RMBTRAP,
  217.    TAG_DONE);
  218.  
  219.    if (!mywindow)
  220.    {
  221.             closeit("Window could not get opened!\n");
  222.      }
  223.  
  224.    buf1=AllocScreenBuffer(myscreen,0,SB_SCREEN_BITMAP);
  225.    if (!buf1)
  226.    {
  227.             closeit("Screenbuffer could not be allocated\n");
  228.      }
  229.  
  230.    buf2=AllocScreenBuffer(myscreen,0,0);
  231.  
  232.    if (!buf2)
  233.    {
  234.             closeit("Screenbuffer could not be allocated\n");
  235.    }
  236.  
  237.    buf3=AllocScreenBuffer(myscreen,0,0);
  238.  
  239.    if (!buf3)
  240.    {
  241.             closeit("Screenbuffer could not be opened!\n");
  242.       }
  243.  
  244.    bm = buf1->sb_BitMap;
  245.  
  246.      mycontext = W3D_CreateContextTags(&CError,
  247.                             W3D_CC_MODEID, ModeID,
  248.                W3D_CC_BITMAP, bm,
  249.                W3D_CC_YOFFSET, 0,
  250.                W3D_CC_DRIVERTYPE, W3D_DRIVER_BEST,
  251.                W3D_CC_FAST, TRUE,
  252.                TAG_DONE);
  253.  
  254.      if ((!mycontext)||(CError != W3D_SUCCESS))
  255.    {
  256.             closeit("W3D Context could not be created.\n");
  257.    }
  258.  
  259. }
  260.  
  261. struct MyData
  262. {
  263.    int id;
  264.       
  265.    float v1_x;
  266.    float v1_y;
  267.    float v2_x;
  268.    float v2_y;
  269.    float v3_x;
  270.    float v3_y;
  271.  
  272.    // Later we will put something there...
  273.    // Scene description or whatever :)
  274. };
  275.  
  276. W3D_Triangle mytriangle;
  277.  
  278. void DrawScene(struct MyData *mydata)
  279. {
  280.     mytriangle.v1.x=mydata->v1_x;
  281.     mytriangle.v1.y=mydata->v1_y;
  282.     mytriangle.v1.color.r=1.0;
  283.     mytriangle.v1.color.g=0.0;
  284.     mytriangle.v1.color.b=0.0;
  285.     mytriangle.v1.color.a=1.0;
  286.  
  287.     mytriangle.v2.x=mydata->v2_x;
  288.     mytriangle.v2.y=mydata->v2_y;
  289.     mytriangle.v2.color.r=0.0;
  290.     mytriangle.v2.color.g=1.0;
  291.     mytriangle.v2.color.b=0.0;
  292.     mytriangle.v2.color.a=1.0;
  293.  
  294.     mytriangle.v3.x=mydata->v3_x;
  295.     mytriangle.v3.y=mydata->v3_y;
  296.     mytriangle.v3.color.r=0.0;
  297.     mytriangle.v3.color.g=0.0;
  298.     mytriangle.v3.color.b=1.0;
  299.     mytriangle.v3.color.a=1.0;
  300.  
  301.     mytriangle.tex=0;
  302.     mytriangle.st_pattern=0;
  303.  
  304.  
  305.       if (W3D_SUCCESS == W3D_LockHardware(mycontext))
  306.       {
  307.       W3D_DrawTriangle(mycontext,&mytriangle);
  308.       W3D_UnLockHardware(mycontext);
  309.     }  
  310. }
  311.  
  312. void initstates()
  313. {
  314.   W3D_SetState(mycontext, W3D_GOURAUD,W3D_ENABLE);
  315.   W3D_SetState(mycontext, W3D_TEXMAPPING,W3D_DISABLE);
  316. }
  317.  
  318. struct MyData mydata;
  319.  
  320. int main()
  321. {
  322.       int running;
  323.       struct IntuiMessage *imsg;
  324.  
  325.       init(640,480,~W3D_FMT_CLUT);
  326.     initstates();
  327.  
  328.     running=1;
  329.  
  330.     mydata.v1_x=0;
  331.     mydata.v1_y=0;
  332.     mydata.v2_x=200;
  333.     mydata.v2_y=0;
  334.     mydata.v3_x=200;
  335.     mydata.v3_y=100;
  336.  
  337.         while(running)
  338.     {
  339.      SwitchBuffer();
  340.      DrawScene(&mydata);
  341.  
  342.          imsg=(struct IntuiMessage *)GetMsg(mywindow->UserPort);
  343.        if (imsg)
  344.      {
  345.          switch(imsg->Class)
  346.           {
  347.             case IDCMP_VANILLAKEY:
  348.                  switch(imsg->Code)
  349.               {
  350.                       case 'q': running=0; break;
  351.                       case '8': mydata.v1_x+=10; break;
  352.                       case '5': mydata.v1_y+=10; break;
  353.                       case '4': mydata.v2_x+=10; break;
  354.                       case '6': mydata.v2_y+=10; break;
  355.                       case '7': mydata.v3_x+=10; break;
  356.                       case '9': mydata.v3_y+=10; break;
  357.              case 'w': mydata.v1_x-=10; break;
  358.              case 'e': mydata.v1_y-=10; break;
  359.              case 'r': mydata.v2_x-=10; break;
  360.              case 't': mydata.v2_y-=10; break;
  361.              case 'z': mydata.v3_x-=10; break;
  362.              case 'u': mydata.v3_y-=10; break;
  363.               }
  364.                 break;
  365.                 
  366.                 printf("%i\n",mydata.v1_x);
  367.       }
  368.            ReplyMsg((struct Message *)imsg);
  369.          }
  370.     }
  371.   closeit("End of Program.\n");
  372. }
  373.